﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; 


namespace StartStop.Helpers
{
    public class UsageCore
    {
        public const string LiveConnectionString = "data source=v2.startstop.me;initial catalog=startstop;persist security info=True;user id=sa;password=PASSWORD;";

        public static StartStop.ServiceResources.Usage[] ReturnAllUsage(Int64 UserID)
        {

            // Select all the window app types
            DataTable oTypesOfApp = Sql.ReturnDataTable("Select * from WindowAppType");  
            

            StartStop.ServiceResources.Usage[] oUsage = new StartStop.ServiceResources.Usage[oTypesOfApp.Rows.Count]; 
            string _sqlCommand = "";
            int _rowCount = 0; 
            foreach (DataRow oRow in oTypesOfApp.Rows)
            {
                
                _sqlCommand = "select sum(ElapsedTimeSeconds) as ElapsedSeconds from USersAppLog where AppiD  in (select Appid from WindowAppList where typeID=" + oRow["TypeID"].ToString() + ") and USerID=" + UserID.ToString();
                 string result = StartStop.Helpers.Sql.ExecuteScalar(_sqlCommand);
                 if (result == "") { result = "0"; }

                 StartStop.ServiceResources.Usage oUse = new ServiceResources.Usage();

                 oUse.TypeOfUse = oRow["TypeName"].ToString();
                 oUse.TotalTime = new TimeSpan(0, 0, Int32.Parse(result));

                 oUsage[_rowCount] = oUse;
                 _rowCount++;
            }


            return oUsage; 
        }
    
    }
}
